#!/bin/sh

COVER="coverage"
OUT="out"
HTML="html"
folders=$(find src/** -maxdepth 1 -type d)

[ ! -e "$COVER" ] && mkdir "$COVER"
[ ! -e "$COVER"/"$OUT" ] && mkdir "$COVER"/"$OUT"
[ ! -e "$COVER"/"$HTML" ] && mkdir "$COVER"/"$HTML"

go test ./src -coverprofile="$COVER"/"$OUT"/src.out
go tool cover -html="$COVER"/"$OUT"/src.out -o "$COVER"/"$HTML"/src.html

for folder in $folders; do
    name=$(echo "$folder" | sed -e "s/\///g")
    go test ./"$folder" -coverprofile="$COVER"/"$OUT"/"$name".out

    if [ -f "$COVER"/"$OUT"/"$name".out ]; then
        lines=$(wc -l "$COVER"/"$OUT"/"$name".out | cut -d ' ' -f1)
        [ "$lines" -gt 1 ] && go tool cover -html="$COVER"/"$OUT"/"$name".out -o "$COVER"/"$HTML"/"$name".html
    fi
done
